package com.csc.dataaccess.dao.impl;

// Generated Jul 16, 2014 5:03:21 PM by Hibernate Tools 4.0.0

import java.util.List;

import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;

import com.csc.dataaccess.dao.PtrnpfDao;
import com.csc.dataaccess.entity.Ptrnpf;

/**
 * Home object for domain model class Ptrnpf.
 * @see com.csc.dataaccess.dao.Ptrnpf
 * @author Hibernate Tools
 */
public class PtrnpfDaoImpl implements PtrnpfDao {

	private static final Log log = LogFactory.getLog(PtrnpfDaoImpl.class);

	private final SessionFactory sessionFactory = getSessionFactory();

	protected SessionFactory getSessionFactory() {
		try {
			return (SessionFactory) new InitialContext()
					.lookup("SessionFactory");
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	public void persist(Ptrnpf transientInstance) {
		log.debug("persisting Ptrnpf instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Ptrnpf instance) {
		log.debug("attaching dirty Ptrnpf instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Ptrnpf instance) {
		log.debug("attaching clean Ptrnpf instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Ptrnpf persistentInstance) {
		log.debug("deleting Ptrnpf instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Ptrnpf merge(Ptrnpf detachedInstance) {
		log.debug("merging Ptrnpf instance");
		try {
			Ptrnpf result = (Ptrnpf) sessionFactory.getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Ptrnpf findById(long id) {
		log.debug("getting Ptrnpf instance with id: " + id);
		try {
			Ptrnpf instance = (Ptrnpf) sessionFactory.getCurrentSession().get(
					"com.csc.dataaccess.dao.Ptrnpf", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Ptrnpf> findByExample(Ptrnpf instance) {
		log.debug("finding Ptrnpf instance by example");
		try {
			List<Ptrnpf> results = sessionFactory.getCurrentSession()
					.createCriteria("com.csc.dataaccess.dao.Ptrnpf")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
}
